/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.persistence.dao;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.lifebudget.app.client.mvc.model.Category;
import org.lifebudget.app.client.mvc.model.CategoryAmount;

import com.google.appengine.api.datastore.Key;

/**
 * The Class CategoryDAO. Data Access Object in for the persistence
 * layer to handle persisting a {@link Category}. Annotated for JPA
 * persistence.
 */
@Entity
public class CategoryDAO {

	/** The key. */
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Key key;
	
	/** The user. */
	private String user;
	
	/** The name. */
	private String name;
	
	/** The description. */
	private String description;
	
	/** The archived. */
	private boolean archived;
	
	/** The default category. */
	private boolean defaultCategory;

	/**
	 * Instantiates a new category dao.
	 */
	public CategoryDAO() {

	}

	/**
	 * Instantiates a new category dao.
	 *
	 * @param category the category
	 */
	public CategoryDAO(Category category) {
		this(category.getName(), category.getDescription());
		this.archived = category.isArchived();
		this.defaultCategory = category.isDefaultCategory();

	}

	/**
	 * Instantiates a new category dao.
	 *
	 * @param name the name
	 * @param description the description
	 */
	public CategoryDAO(String name, String description) {
		super();
		this.name = name;
		this.description = description;
	}

	/**
	 * Gets the description.
	 *
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Gets the key.
	 *
	 * @return the key
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * Gets the name.
	 *
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Gets the user.
	 *
	 * @return the user
	 */
	public String getUser() {
		return user;
	}

	/**
	 * Checks if is archived.
	 *
	 * @return true, if is archived
	 */
	public boolean isArchived() {
		return archived;
	}

	/**
	 * Sets the archived.
	 *
	 * @param archived the new archived
	 */
	public void setArchived(boolean archived) {
		this.archived = archived;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the new description
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * Sets the fields from model object.
	 *
	 * @param category the new fields from model object
	 */
	public void setFieldsFromModelObject(Category category) {
		this.name = category.getName();
		this.description = category.getDescription();
		this.archived = category.isArchived();
		this.defaultCategory = category.isDefaultCategory();
	}

	/**
	 * Sets the key.
	 *
	 * @param key the new key
	 */
	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * Sets the name.
	 *
	 * @param name the new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Sets the user.
	 *
	 * @param user the new user
	 */
	public void setUser(String user) {
		this.user = user;
	}

	/**
	 * To model object.
	 *
	 * @return the category
	 */
	public Category toModelObject() {
		Category c = new Category(key.getId(), name, description, archived);
		c.setDefaultCategory(this.defaultCategory);
		return c;
	}

	/**
	 * Sets the default category.
	 *
	 * @param defaultCategory the new default category
	 */
	public void setDefaultCategory(boolean defaultCategory) {
		this.defaultCategory = defaultCategory;
	}

	/**
	 * Checks if is default category.
	 *
	 * @return true, if is default category
	 */
	public boolean isDefaultCategory() {
		return defaultCategory;
	}

}
